#include "plist.h"
#include "WingplaneConfig.h"

WingplaneConfig::PTR gWingplaneConfig;

bool WingplaneConfig::getStarLevelMaxLevel(int starLevel, int& normalLevel) const
{
    auto it = mStarLevelNormalLevelMax.find(starLevel);
    if (it != mStarLevelNormalLevelMax.end())
    {
        normalLevel = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

bool WingplaneConfig::getUpgradeLevelConfig(int level, UpgradeWingplaneLevelConfig& config) const
{
    auto it = mUpgradeWingplaneLevelConfig.find(level);
    if (it != mUpgradeWingplaneLevelConfig.end())
    {
        config = it->second;
        return true;
    }
    else
    {
        return false;
    }
}

void WingplaneConfig::loadAllConfig()
{
    try
    {
        AnyValue* pv = parsePlistByFileName("UpgradeWingplaneNormalLevel.plist");
        if (pv != nullptr)
        {
            auto rootDict = pv->asDict();
            {
                auto StarLevelMaxNormalLevelConfig = rootDict->getValue().find("StarLevelMaxNormalLevel")->second->asDict();
                for (auto v : StarLevelMaxNormalLevelConfig->getValue())
                {
                    int starLevel = 0;
                    sscanf(v.first.c_str(), "%d", &starLevel);
                    mStarLevelNormalLevelMax[starLevel] = v.second->asInt()->getValue();
                }
            }

            {
                auto NormalLevelExpConfig = rootDict->getValue().find("UpgradeCost")->second->asDict();
                for (auto v : NormalLevelExpConfig->getValue())
                {
                    UpgradeWingplaneLevelConfig dataConfig;

                    int normalLevel = 0;
                    sscanf(v.first.c_str(), "%d", &normalLevel);
                    const PntDict* oneCost = v.second->asDict();
                    const PntDict::VALUE_TYPE& values = oneCost->getValue();

                    int money = values.find("money")->second->asInt()->getValue();
                    int diamond = values.find("diamond")->second->asInt()->getValue();

                    dataConfig.money = money;
                    dataConfig.diamond = diamond;

                    const PntArray::VALUE_TYPE& items = values.find("items")->second->asArray()->getValue();
                    for (auto& item : items)
                    {
                        const PntDict::VALUE_TYPE& values = item->asDict()->getValue();
                        int id = values.find("id")->second->asInt()->getValue();
                        int num = values.find("num")->second->asInt()->getValue();

                        dataConfig.needItems.push_back({ id, num });
                    }

                    mUpgradeWingplaneLevelConfig[normalLevel] = dataConfig;
                }
            }
        }
    }
    catch (PlistParseError& e)
    {

    }
}